package com.changgou.service.goods.controller;


import com.changgou.commom.pojo.PageResult;
import com.changgou.commom.pojo.Result;
import com.changgou.commom.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有
     */
    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result<Brand> findAById(@PathVariable Integer id) {
        Brand brand = brandService.findAById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 新增品牌
     */
    @PostMapping
    public void add(@RequestBody Brand brand) {
        brandService.add(brand);
        new Result<>(true, StatusCode.OK, "添加成功");
    }

    /**
     * 修改品牌
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除品牌
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 条件查询
     */
    @GetMapping(value = "/findList")
    public Result findList(@RequestParam Map<String, Object> map) {
        List<Brand> list = brandService.findList(map);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页查询
     */
    @GetMapping(value = "/findPage/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}
